<template>
	<view>
		<navbar title="移动文件">
		</navbar>
		<!-- 路径 -->
		<uni-breadcrumb separator="/" style="background-color: #f5f5f5;padding: 5px;">
			<uni-breadcrumb-item v-for="(item,index) in path" :key="index">
				<view @click="back(item.to,index)">
					{{item.name}}
				</view>
			</uni-breadcrumb-item>
		</uni-breadcrumb>
		<!-- 文件列表 -->
		<scroll-view scroll-y="true" @scrolltolower="tolower" class="list">
			<unicloud-db ref="udb" v-slot:default="{data, pagination, loading, error,hasMore, options}"
				collection="item" orderby="top desc,last_time desc, weight asc,time desc" loadtime="manual"
				:where="itemwhere">
				<view v-if="hasMore||data.length>0" v-for="(item,index ) in data" :key="index"
					@click="to(item.title,item._id)">
					<u-transition :show="true">
						<item_file :item="item">
						</item_file>
					</u-transition>
				</view>
				<u-loadmore v-if="loading" loadingText="拼命加载中" status="loading" />
				<view v-if="data.length==0&&!loading&&!hasMore" style="text-align: center;margin-top: 20px;">
					<u-empty mode="data" text="没有内容~">
					</u-empty>
				</view>
			</unicloud-db>
		</scroll-view>
		<view
			style="height: 60px;position: fixed;bottom: 0px;width: 100%;display: flex;justify-content: space-between;align-items: center;background-color: white;">
			<view style="display: flex;align-items: center;margin: 0px 10px;" @click="add">
				<uni-icons type="folder-add" size="35" style="margin-right: 5px;"></uni-icons>新建文件夹
			</view>
			<button type="primary" style="margin: 10px;line-height: 40px;font-size: 16px;border-radius: 20px;"
				@click="move">移动到此</button>
		</view>
		<!-- 创建合集 -->
		<u-popup round="10" bgColor="#fff" :show="show" @close="show=false" mode="center">
			<view style="padding: 15px;width: 250px;">
				<h3>创建集合</h3>
				<u--input style="margin-top: 10px;" placeholder="请输入文件夹标题" border="surround"
					v-model="addset.title"></u--input>
				<u--textarea style="margin-top: 10px;" v-model="addset.content" placeholder="请输入文件夹介绍"></u--textarea>
				<view style="display: flex;justify-content: space-between;align-items: center;margin-top: 10px;">
					<p style="font-size: 14px;display: flex;align-items: center;">
						<uni-icons type="locked-filled" size="20" color="#85bcff" /><span>启用访问密码</span>
					</p>
					<u-switch v-model="addset.password_mode" @change="setswitch"></u-switch>
				</view>
				<u--input v-if="addset.password_mode" style="margin-top: 10px;" placeholder="请输入文件夹密码" border="surround"
					v-model="addset.password"></u--input>
				<u-button style="margin-top: 10px;" type="primary" text="创建" @click="addfile"></u-button>
			</view>
		</u-popup>
	</view>
</template>

<script>
	const db = uniCloud.database();
	import ase from '@/common/ar-aes/ar-aes.js'
	export default {
		data() {
			return {
				show: false,
				addset: {},
				itemwhere: null,
				user_id: db.getCloudEnv('$cloudEnv_uid'),
				id: null,
				path: [{
					name: "主页",
					to: "/"
				}],
				up: "/"
			};
		},
		watch: {
			up(val) {
				this.up = val
				this.itemwhere = `user_id==$cloudEnv_uid&&up=='${val}'&&type=='file'`
				this.$nextTick(() => {
					this.$refs.udb.loadData({
						clear: true
					})
				})
			}
		},
		methods: {
			setswitch(e) {
				this.addset.password_mode = e
			},
			async addfile() {
				if (this.addset.title == '' || this.addset.title == null) {
					this.message('标题不完整', 'error')
					return false
				}
				if (this.addset.password_mode) {
					if (this.addset.password == null || this.addset.password.length == 0) {
						this.message('密码为空', 'error')
						return false
					}
				}
				this.addset['time'] = new Date()
				this.addset['user_id'] = this.user_id
				this.addset['weight'] = 1
				this.addset['openup'] = "file_jingdian"
				db.collection("item").add(this.addset).then(() => {
					this.$refs.udb.loadData({
						clear: true
					})
					this.show = false
					this.message('创建成功')
				})
			},
			add() {
				this.addset = {
					title: null,
					content: "",
					password_mode: false,
					password: null,
					type: 'file',
					up: this.up
				}
				this.show = true
			},
			async move() {
				if (this.id == null) {
					return null
				}
				this.$refs.udb.update(this.id, {
					up: this.up
				}, {
					needLoading: false,
					toastTitle: '移动成功',
					success: (res) => {
						uni.$emit("moveFile", this.id)
						setTimeout(function() {
							uni.switchTab({
								url: getCurrentPages()[0].route.replace("pages", "..")
							})
						}, 500)
					}
				})
			},
			to(title, up) {
				this.path.push({
					name: title,
					to: up
				})
				this.up = up
			},
			back(e, index) {
				if (e == this.up) {
					return false
				}
				this.path.splice(index + 1)
				this.up = e
			},
			tolower() {
				this.$refs.udb.loadMore()
			}
		},
		onLoad(op) {
			this.id = op.id
			this.itemwhere = `user_id==$cloudEnv_uid&&up=='/'&&type=='file'&&_id!='${op.id}'`
			this.$nextTick(() => {
				this.$refs.udb.loadData({
					clear: true
				})
			})
		},
		onPullDownRefresh() {
			this.$refs.udb.loadData({
				clear: true
			}, () => {
				uni.stopPullDownRefresh()
			})
		},
		onBackPress() {
			if (this.up != "/") {
				this.up = this.path[this.path.length - 2].to
				this.path.splice(this.path.length - 1, 1)
				return true
			}
		}
	}
</script>
<style lang="scss">
	.list {
		background-color: white;
		border-radius: 15px 15px 0 0;
		padding-top: 5px;
		height: calc(100vh - 134px - var(--status-bar-height));
	}
</style>